package com.example.musictutor.services;

import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;

import com.example.musictutor.R;
import com.example.musictutor.domain.Song;

public class Metronome {

	private MediaPlayer mediaPlayer;
	private Context context;

	public Metronome(Context context) {
		this.context = context;
	}
	
	public void playMetronomeSound(boolean first) {
		if (first) {
			playMetronomeFirstBeat();
		} else {
			stopMediaPlayer();
			if (mediaPlayer == null) {
				mediaPlayer = new MediaPlayer();

				try {
					AssetFileDescriptor afd = null;
					afd = context.getResources().openRawResourceFd(
							R.raw.metronome);

					if (afd != null && mediaPlayer != null) {
						mediaPlayer.setDataSource(afd.getFileDescriptor(),
								afd.getStartOffset(), afd.getDeclaredLength());
						mediaPlayer
								.setOnPreparedListener(new OnPreparedListener() {

									@Override
									public void onPrepared(MediaPlayer mp) {
										mp.setVolume(0.35f, 0.35f);
										mp.start();
									}
								});
						mediaPlayer.setOnErrorListener(new OnErrorListener() {

							@Override
							public boolean onError(MediaPlayer mp, int what,
									int extra) {
								Log.e("Hein MediaPlayer",
										"ErrorListener Triggered");
								if (mp != null) {
									mp.release();
									mp = null;
									mediaPlayer = null;
								}
								return false;
							}
						});
						mediaPlayer.prepareAsync();
					}

				} catch (IllegalArgumentException e) {
					Log.e("Player", e.getStackTrace().toString());
				} catch (SecurityException e) {
					Log.e("Player", e.getStackTrace().toString());
				} catch (IllegalStateException e) {
					Log.e("Player", e.getStackTrace().toString());
				} catch (IOException e) {
					Log.e("Player", e.getStackTrace().toString());
				}
			} else {
				mediaPlayer.start();
			}
		}
	}

	public void playMetronomeFirstBeat() {
		MediaPlayer mp = new MediaPlayer();

		try {
			AssetFileDescriptor afd = null;

			afd = context.getResources().openRawResourceFd(R.raw.metronome2);

			if (afd != null && mp != null) {
				mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
						afd.getDeclaredLength());
				mp.setOnPreparedListener(new OnPreparedListener() {

					@Override
					public void onPrepared(MediaPlayer mp) {
						mp.setVolume(0.25f, 0.25f);
						mp.start();
					}
				});
				mp.setOnErrorListener(new OnErrorListener() {

					@Override
					public boolean onError(MediaPlayer mp, int what, int extra) {
						Log.e("Hein MediaPlayer", "ErrorListener Triggered");
						if (mp != null) {
							mp.release();
							mp = null;
						}
						return false;
					}
				});
				mp.setOnCompletionListener(new OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp) {
						if (mp != null) {
							mp.release();
							mp = null;
						}
					}
				});
				mp.prepareAsync();
			}

		} catch (IllegalArgumentException e) {
			Log.e("Player", e.getStackTrace().toString());
		} catch (SecurityException e) {
			Log.e("Player", e.getStackTrace().toString());
		} catch (IllegalStateException e) {
			Log.e("Player", e.getStackTrace().toString());
		} catch (IOException e) {
			Log.e("Player", e.getStackTrace().toString());
		}
	}

	public void stopMediaPlayer() {
		if (mediaPlayer != null) {

			if (mediaPlayer.isPlaying())
				mediaPlayer.stop();

			mediaPlayer.seekTo(0);
		}
	}
}
